//
//  SettingPwdViewController.m
//  cherryproduct
//
//  Created by Enter zhao on 14-8-19.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "SettingPwdViewController.h"

@interface SettingPwdViewController ()

@end

@implementation SettingPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"输入新密码"];
    self.myView.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.pwdField.secureTextEntry=YES;
    self.rePwdField.secureTextEntry=YES;
    self.pwdField.delegate=self;
    self.rePwdField.delegate=self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.pwdField resignFirstResponder];
    [self.rePwdField resignFirstResponder];
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.pwdField resignFirstResponder];
    [self.rePwdField resignFirstResponder];
    return YES;
}

- (IBAction)okAction:(id)sender {
    
    NSString * str_pwd = self.pwdField.text;
    NSString * str_re_pwd = self.rePwdField.text;
    if (str_pwd == nil || [str_pwd isEqual:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请输入新密码!"];
        return;
    }
    if (str_re_pwd == nil || [str_re_pwd isEqual:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请输入确认密码!"];
        return;
    }
    if (![str_pwd isEqualToString:str_re_pwd]) {
        [self showAlertView:@"温馨提示" subtitle:@"两次输入密码不一致!"];
        return;
    }
    
    [self.view startLoadingAnimation];
    
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       _pwdField.text, @"pwd",
                                       _rePwdField.text, @"re_pwd",nil];
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/reset_pwd_phone" paramDictionary:requestDic onCompletion:^(NSString *responseString) {
        
        [self.view stopLoadingAnimation];
        
        @try {
            
            NSDictionary* dic = [responseString JSONValue];
            if ([[dic objectForKey:@"status"] intValue] == 1) {
                
                __block URBAlertView *alertView = [URBAlertView dialogWithTitle:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
                alertView.blurBackground = YES;
                //	[alertView addButtonWithTitle:@"Close"];
                [alertView addButtonWithTitle:@"确定"];
                [alertView setHandlerBlock:^(NSInteger buttonIndex, URBAlertView *alertView) {
                    NSLog(@"button tapped: index=%i", buttonIndex);
                    [alertView hideWithCompletionBlock:^{
                        [self popToViewController:@"LoginViewController"];
                        // stub
                    }];
                }];
                [alertView showWithAnimation:URBAlertAnimationFlipHorizontal];
                
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        } @catch (NSException *exception) {
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
        }
        
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        
       [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
}
@end









